var Generator = require('yeoman-generator');

module.exports = class extends Generator{
  // 询问创建项目需要的基础信息
  prompting(){
    return this.prompt([
      {
        type: 'rawlist',
        name: 'appType',
        message: '请选择创建项目类型？',
        choices: ['vue', 'react', 'h5', 'mini'],
        default: 0
      },
      {
        type:'input',
        name: 'appName',
        message: '请输入项目名称：',
        default: this.appname // appname 为当前项目生成目录文件的名字
      }
    ])
    .then((answers)=>{
      console.log(answers);
      this.answers = answers
    })
  }
  
  // 基于选择模板构建对象
  writingTemplateFile() {
    if (this.answers.appType === 0) {
      // vue
    } else if (this.answers.appType === 1) {
      // react
    } else if (this.answers.appType === 2) {
      // h5
    } else if (this.answers.appType === 3) {
      // mini
    }
    const templates = [
      'package.json',
      'public/favicon.ico',
      'public/index.html',
      'src/assets/logo.png',
      'src/components/HelloWorld.vue',
      'src/App.vue',
      'src/main.js',
      'babel.config.js',
      'README.md',
    ]
    templates.forEach(item=>{
      // item => 每个文件路径
      this.fs.copyTpl(
        this.templatePath(item),
        this.destinationPath(item),
        this.answers
      )
    }) 
  }

  installingProjectPackage(){
    const pkgJson = {
      devDependencies: {
        "webpack": '',
        "webpack-cli": '',
        "webpack-dev-server": '',
        "babel": '',
        "@babel/core": '',
        "@babel/preset-env": '',
      }
    };
    // Extend or create package.json file in destination path
    this.fs.extendJSON(this.destinationPath('package.json'), pkgJson);
  }

}